package com.wbp.cms.core.product.service;

import java.util.List;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.google.common.base.Preconditions;
import com.wbp.cms.client.product.domain.ProductProfitDO;
import com.wbp.cms.client.product.query.ProductProfitQuery;
import com.wbp.cms.client.product.service.ProductProfitService;
import com.wbp.cms.client.result.Result;
import com.wbp.cms.core.exception.ServiceException;
import com.wbp.cms.core.product.dao.ProductProfitDAO;


/**
 * @Description:
 * @Department:
 * @author andy.lhc
 * @date 2016年3月9日 下午2:23:05
 */
@Service("productProfitService")
public class ProductProfitServiceImpl implements ProductProfitService {
   
  /**
   * 
   */
  private static final long serialVersionUID = 769641518813255508L;

  private static final Logger  logger = Logger.getLogger(ProductProfitServiceImpl.class) ;
  @Resource
  private  ProductProfitDAO  productProfitDAO ;
  
  
  
  @Override
  public Result<Long> addProductProfit(ProductProfitDO productProfitDO) {
         // 参数的校验放在controller层去做,service层只做必要的参数验证
         Preconditions.checkArgument(productProfitDO != null, "参数不合法");

         return Result.buildSuccess(productProfitDAO.insert(productProfitDO));
    
  }
  
  @Override
  public Result<Integer>  addProductProfit(List<ProductProfitDO> productProfitDO){
    //try {
            int count = 0 ;
            Preconditions.checkArgument(productProfitDO != null, "productProfitDO为null");
            Preconditions.checkArgument(productProfitDO.size() != 0, "productProfitDO为空");
            for(ProductProfitDO pp : productProfitDO){
              if(productProfitDAO.insert(pp) >0 )
                  count++ ;
            }
           
             return Result.buildSuccess(Integer.valueOf(count));
         /*} catch (Exception e) {
              logger.error("addProductProfit exception,productProfitDO:" + productProfitDO, e);
              return Result.buildError(e.getMessage());
         }*/
  } 

   
  @Override
  public Result<Boolean> modifyProductProfit(ProductProfitDO productProfitDO) {
   
          Preconditions.checkArgument(productProfitDO != null, "参数不合法");

          return Result.buildSuccess(productProfitDAO.update(productProfitDO) >0 );
  }

  
  @Override
  public Result<ProductProfitDO> loadProductProfit(Long productProfitId) {
    try {
           Preconditions.checkNotNull(productProfitId, "productProfitId:"+productProfitId+",不能为空") ;

           return Result.buildSuccess(productProfitDAO.load(productProfitId) );
       } catch (Exception e) {
            logger.error("loadProductProfit exception,productProfitId:" + productProfitId, e);
            return Result.buildError(e.getMessage());
       }
  }


  
  @Override
  public Result<List<ProductProfitDO>> loadProductProfitByProductId(Long productId) {
       
    Preconditions.checkNotNull(productId, "productId:"+productId+",不能为空") ;
    
    ProductProfitQuery  productProfitQuery =new ProductProfitQuery();
    productProfitQuery.setProductId(productId);
    return Result.buildSuccess(productProfitDAO.query(productProfitQuery) );
  }

  
  @Override
  public Result<Integer> removeProductProfit(List<ProductProfitDO> productProfitDO) {
     
    Preconditions.checkArgument(productProfitDO !=null , "productProfitDO 为null");
    Preconditions.checkArgument(productProfitDO.size() !=0 , "productProfitDO 为空");
    
    for(ProductProfitDO pp : productProfitDO){
        if( productProfitDAO.delete(pp.getId()) < 0)
           throw new ServiceException("delete ProductProfitDO失败,productProfitId:"+pp.getId()) ;
    }
    
    return Result.buildSuccess(productProfitDO.size());
  }

   
  @Override
  @Transactional
  public Result<Integer> removeProductProfit(Long productId) {
    Preconditions.checkArgument(productId !=null  && productId >0, "productId应>0");
    int r = this.productProfitDAO.deleteByProductId(productId) ;
    if(r <=0)
      throw new ServiceException("delete ProductProfitDO失败,productId:"+productId) ;
    return Result.buildSuccess(r);
  }
  
  

}
